/* eslint-disable no-undef */
import axios from 'axios'
const BaseUrl = import.meta.env.VITE_RES_URL || "/proxy/"
const service = axios.create({
    // 联调
    // baseURL: process.env.NODE_ENV === 'mock' ? `/api/` : '/proxy/',
    baseURL:BaseUrl,
    // 是否跨站点访问控制请求
    withCredentials: false,
    timeout: 3000,
    // 请求数据拦截
    // transformRequest: [
    //     (data) => {
    //         return data
    //     },
    // ],
    validateStatus() {
        // 使用async-await，处理reject情况较为繁琐，所以全部返回resolve，在业务代码中处理异常
        return true
    },
    // 响应数据拦截
    transformResponse: [
        (data) => {
            if (typeof data === 'string' && data.startsWith('{') || data.startsWith('[')) {
                data = JSON.parse(data)
            }
            return data
        },
    ],
})

// 请求拦截器
service.interceptors.request.use(
    (config) => {
        // 手动添加jwt-token
        // if (store.getters["userinfo/jwttoken"]) {
        //     config.headers['Authorization'] = store.getters["userinfo/jwttoken"]
        // }
        // 手动添加密钥ID
        // if(store.state.encryption.encryptionID){
        //     config.headers["encryptionID"] = store.state.encryption.encryptionID
        // }
        return config
    },
    (error) => {
        // console.log("请求拦截2")
        // 错误抛到业务代码
        return Promise.resolve(error)
    }
)

// 响应拦截器
service.interceptors.response.use(
    (response) => {
        // console.log("响应拦截1")
        // 状态码在200多是正常回复，超过300的由拦截器处理
        if (response.status === 404) {
            console.log(404)
        } else if (response.status >= 500) {
            console.log(500)
        } else if (response.status >= 300) {
            Object.values(response.data).forEach((item) => {
                if (Array.isArray(item)) {
                    item.forEach((yuansu) => {
                        console.log(yuansu)
                    })
                } else {
                    console.log(item)
                }
            })
        }
        return response

    },
    (error) => {
        // console.log("响应拦截2")
        return Promise.resolve(error)
    }
)

export function json(url, data=null) {
    return service({
        method: "post",
        url: url,
        data: data,
        headers: {'Content-Type': 'application/json;charset=utf-8',}
    })
}

export function update(url,data=null,id = null) {
    if (id) {
        return service({
            method: "patch",
            url: url + id + "/",
            data: data,
            headers: {'Content-Type': 'application/json;charset=utf-8',}
        })
    } else {
        return service({
            method: "patch",
            url: url,
            data: data,
            headers: {'Content-Type': 'application/json;charset=utf-8',}
        })
    }
}

export function shanchu(url, data=null,id=null) {
    if (id) {
        return service({
            method: "delete",
            url: url + id + "/",
            params: data,
            headers: {'Content-Type': 'application/json;charset=utf-8',}
        })
    } else {
        return service({
            method: "delete",
            url: url,
            params: data,
            headers: {'Content-Type': 'application/json;charset=utf-8',}
        })
    }
}

export function get(url, data=null, id = null) {
    return service({
        method: "get",
        url: url + id + "/",
        params: data,
    })

}

export function list(url, data=null) {
    return service({
        method: "get",
        url: url,
        params: data,
    })
}

export function form(url, data=null) {
    return service({
        method: "post",
        url: url,
        data: data,
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        // transformRequest: [function (data) {
        //     let ret = ''
        //     for (const it in data) {
        //         ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
        //     }
        //     return ret
        // }]
    })
}

export default service
